//
//  mm_promoteSelPopUpViewController.m
//  HondaPro_2
//
//  Created by Eric on 13-12-23.
//  Copyright (c) 2013年 glendale. All rights reserved.
//

#import "mm_promoteSelPopUpViewController.h"
#import "getJobSheetViewController.h"
#import "MainMenuDataModel.h"
#import "mm_selfDefinedPromoteSelTableViewCell.h"
#import "HelpClass.h"

@interface mm_promoteSelPopUpViewController ()

@end

@implementation mm_promoteSelPopUpViewController

@synthesize arrPromoteSelectedList = _arrPromoteSelectedList;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self.view setFrame:CGRectMake(0, 0, 860, 600)];
    
    MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
    _arrAllTheSelRowList = [NSMutableArray arrayWithCapacity:[[modelIns.dicPromoteList objectForKey:@"promoteList"] count]];
    for (int i = 0; i<[[modelIns.dicPromoteList objectForKey:@"promoteList"] count]; i++)
    {
        /*
         如果选中行 存状态为YES，否则存状态为NO
         */
        [_arrAllTheSelRowList addObject:@"NO"];
    }
    self.arrPromoteSelectedList = [NSMutableArray array];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)OKBtnPressed:(UIButton *)sender
{
    self.arrPromoteSelectedList = [NSMutableArray array];
    
    MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
    for (int i = 0; i<[[modelIns.dicPromoteList objectForKey:@"promoteList"] count]; i++)
    {
        NSString *selStateStrTmp = [_arrAllTheSelRowList objectAtIndex:i];
        if ([selStateStrTmp isEqualToString:@"YES"])
        {
            [self.arrPromoteSelectedList addObject:[[modelIns.dicPromoteList objectForKey:@"promoteList"] objectAtIndex:i]];
        }
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:MM_PROMOTE_LIST_QUIT_POP_UP_VIEW_MSG_ object:nil];
}

- (IBAction)closeBtnPressed:(UIButton *)sender
{
    self.arrPromoteSelectedList = [NSMutableArray array];
    [[NSNotificationCenter defaultCenter] postNotificationName:MM_PROMOTE_LIST_QUIT_POP_UP_VIEW_MSG_ object:nil];
}

#pragma mark - tableView dataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[[MainMenuDataModel sharedMainMenuDataModel].dicPromoteList objectForKey:@"promoteList"] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"mm_selfDefinedPromoteSelTableViewCell_identifier";
    mm_selfDefinedPromoteSelTableViewCell *cell = (mm_selfDefinedPromoteSelTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellID];
    if (nil == cell)
    {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"mm_selfDefinedPromoteSelTableViewCell" owner:self options:nil] objectAtIndex:0];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    //cell的风格是：浅浅灰色和浅灰色间隔出现，第一行是浅灰色
    NSInteger row = [indexPath row];
    switch (row%2)
    {
        case 0:
            //浅灰色
            cell.contentView.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
            break;
        case 1:
            //浅浅灰色
            cell.contentView.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1];
            break;
            
            default:
            break;
    }
    
    //配置cell
    /*
     {
     "serialNo":"1",
     "promoteTypeName":"保养1",
     "promoteDate":"2013-06",
     "shortMsgConnectDay":"2013-12-12",
     "letterConnectDay":"2013-12-12",
     "telConnectDayOne":"2013-12-12",
     "telConnectDayTwo":"2013-12-12",
     "telConnectDayThree":"2013-12-12",
     "precontractDay":"2013-12-12",
     "executeDay":"2013-12-12"
     }
     */
    MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
    NSDictionary *dicTmp = [[modelIns.dicPromoteList objectForKey:@"promoteList"] objectAtIndex:[indexPath row]];
    
    cell.promoteTypeNameLbl.text = [dicTmp objectForKey:@"promoteTypeName"];
    cell.promoteDateLbl.text = [dicTmp objectForKey:@"promoteDate"];
    cell.shortMsgConnectDayLbl.text = [dicTmp objectForKey:@"shortMsgConnectDay"];
    cell.letterConnectDayLbl.text = [dicTmp objectForKey:@"letterConnectDay"];
    cell.telConnectDayOneLbl.text = [dicTmp objectForKey:@"telConnectDayOne"];
    cell.telConnectDayTwoLbl.text = [dicTmp objectForKey:@"telConnectDayTwo"];
    cell.telConnectDayThreeLbl.text = [dicTmp objectForKey:@"telConnectDayThree"];
    cell.precontractDayLbl.text = [dicTmp objectForKey:@"precontractDay"];
    cell.executeDayLbl.text = [dicTmp objectForKey:@"executeDay"];
    
    return cell;
}

#pragma mark - tableView delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    mm_selfDefinedPromoteSelTableViewCell *cell = (mm_selfDefinedPromoteSelTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
    if ([cell.selBtn.titleLabel.text isEqualToString:@"对号"])
    {
        [cell.selBtn setBackgroundImage:[UIImage imageNamed:@"对号框.png"] forState:UIControlStateNormal];
        [cell.selBtn setTitle:@"对号框" forState:UIControlStateNormal];
        [_arrAllTheSelRowList setObject:@"NO" atIndexedSubscript:[indexPath row]];
    }
    else
    {
        [cell.selBtn setBackgroundImage:[UIImage imageNamed:@"对号.png"] forState:UIControlStateNormal];
        [cell.selBtn setTitle:@"对号" forState:UIControlStateNormal];
        [_arrAllTheSelRowList setObject:@"YES" atIndexedSubscript:[indexPath row]];
    }
}

@end
